//


#import "AM_ProgressTool.h"
#import <MBProgressHUD/MBProgressHUD.h>

#define HUDProgressSrcName(file) [@"HUDProgress.bundle" stringByAppendingPathComponent:file]

@implementation AM_ProgressTool

static const NSTimeInterval kMinHideTime = 1.0f;

+ (UIWindow *)keyWindow
{
    for (UIWindow *window in [[UIApplication sharedApplication] windows])
    {
        if (window.isKeyWindow && window.windowLevel == UIWindowLevelNormal)
        {
            return window;
        }
    }
    return [[[UIApplication sharedApplication] windows] firstObject];
}

// 计算文字长度
+ (NSTimeInterval)calculateHideTime:(NSString *)text
{
    return text.length * 0.04 + kMinHideTime;
}

+ (void)showOnlyText:(NSString *)text
{
    [self showOnlyText:text inView:[self keyWindow]];
}

+ (void)showOnlyText:(NSString *)text inView:(UIView *)inView
{
    [self dismiss];
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:inView];
    [inView addSubview:hud];
    
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabel.text = text;
    
    hud.margin = 16.0f;
    hud.detailsLabel.font = [UIFont boldSystemFontOfSize:15.0f];
    hud.animationType = MBProgressHUDAnimationFade;
    hud.userInteractionEnabled = NO;
    
    [hud showAnimated:YES];
    [hud hideAnimated:YES afterDelay:[self calculateHideTime:text]];
}


+ (void)dismiss
{
    [self dismissFormView:[self keyWindow] animated:YES];
}

+ (void)dismissFormView:(UIView *)view animated:(BOOL)animated
{
    [MBProgressHUD hideHUDForView:view animated:animated];
}

@end
